using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using WMS.RealTime.Domain.DTOs;
using WMS.RealTime.IServers.Interfaces;

namespace WMS.RealTime.Servers.Services
{
    public class WMSDataService : IWMSDataService
    {
        private readonly HttpClient _httpClient;
        private readonly ILogger<WMSDataService> _logger;
        private readonly IConfiguration _configuration;
        private readonly string _wmsApiBaseUrl;

        public WMSDataService(HttpClient httpClient, ILogger<WMSDataService> logger, IConfiguration configuration)
        {
            _httpClient = httpClient;
            _logger = logger;
            _configuration = configuration;
            _wmsApiBaseUrl = _configuration["WMS:ApiBaseUrl"] ?? "http://localhost:5000";
        }

        public async Task<DailyOverview> GetTodayOverviewAsync()
        {
            _logger.LogInformation("从WMS服务获取今日概况数据");
            // 这里调用真实WMS API
            return new DailyOverview
            {
                Date = DateTime.Today,
                DocumentCount = 52,
                InboundQuantity = 602,
                InboundAmount = 2000.00m,
                OutboundQuantity = 602,
                OutboundAmount = 2000.00m
            };
        }

        public async Task<DailyOverview> GetYesterdayOverviewAsync()
        {
            _logger.LogInformation("从WMS服务获取昨日概况数据");
            return new DailyOverview
            {
                Date = DateTime.Today.AddDays(-1),
                DocumentCount = 48,
                InboundQuantity = 580,
                InboundAmount = 1950.00m,
                OutboundQuantity = 590,
                OutboundAmount = 1980.00m
            };
        }

        public async Task<InventoryProportion> GetInventoryProportionAsync()
        {
            _logger.LogInformation("从WMS服务获取库存占比数据");
            var categories = new List<CategoryData>
            {
                new() { Name = "原材料", Quantity = 1250, Amount = 2500.00m },
                new() { Name = "半成品", Quantity = 890, Amount = 1800.00m },
                new() { Name = "成品", Quantity = 1560, Amount = 3200.00m },
                new() { Name = "包装物", Quantity = 680, Amount = 1200.00m },
                new() { Name = "辅料", Quantity = 420, Amount = 800.00m },
                new() { Name = "其他", Quantity = 200, Amount = 400.00m }
            };

            var warehouses = new List<WarehouseData>
            {
                new() { WarehouseName = "仓库一", Quantity = 567, Amount = 539.00m },
                new() { WarehouseName = "仓库二", Quantity = 678, Amount = 920.00m },
                new() { WarehouseName = "仓库三", Quantity = 456, Amount = 995.00m },
                new() { WarehouseName = "仓库四", Quantity = 789, Amount = 687.00m },
                new() { WarehouseName = "仓库五", Quantity = 567, Amount = 178.00m },
                new() { WarehouseName = "仓库六", Quantity = 345, Amount = 867.00m },
                new() { WarehouseName = "仓库七", Quantity = 678, Amount = 120.00m }
            };

            return new InventoryProportion
            {
                TotalQuantity = categories.Sum(c => c.Quantity),
                TotalAmount = categories.Sum(c => c.Amount),
                Categories = categories,
                Warehouses = warehouses
            };
        }

        public async Task<InboundStatistics> GetInboundStatisticsAsync(string timeRange = "7")
        {
            _logger.LogInformation($"从WMS服务获取入库统计数据，时间范围：{timeRange}");
            var dailyData = new List<DailyData>();
            var days = int.Parse(timeRange);
            
            for (int i = days - 1; i >= 0; i--)
            {
                dailyData.Add(new DailyData
                {
                    Date = DateTime.Today.AddDays(-i),
                    Quantity = Random.Shared.Next(50, 200),
                    Amount = Random.Shared.Next(500, 1500)
                });
            }

            return new InboundStatistics
            {
                DailyData = dailyData,
                TimeRange = $"最近{timeRange}天"
            };
        }

        public async Task<TodoItems> GetTodoItemsAsync()
        {
            _logger.LogInformation("从WMS服务获取待办事项数据");
            return new TodoItems
            {
                PendingApproval = new PendingApproval
                {
                    InboundDocuments = 2,
                    OutboundDocuments = 3,
                    TransferDocuments = 5,
                    PurchaseDocuments = 5,
                    SalesDocuments = 2,
                    PurchaseReturns = 3,
                    SalesReturns = 4
                },
                PendingExecution = new PendingExecution
                {
                    PendingInbound = 2,
                    PendingOutbound = 3
                }
            };
        }

        public async Task<AlertData> GetAlertDataAsync()
        {
            _logger.LogInformation("从WMS服务获取告警数据");
            return new AlertData
            {
                InventoryAboveLimit = 2,
                InventoryBelowLimit = 2,
                ExpiringGoods = 2,
                ExpiredGoods = 2
            };
        }

        public async Task<RankingData> GetRankingDataAsync()
        {
            _logger.LogInformation("从WMS服务获取排名数据");
            var inboundRanking = Enumerable.Range(1, 7).Select(i => new RankingItem
            {
                Rank = i,
                ProductName = $"货品{i}",
                Quantity = 50,
                Amount = 200.00m,
                Count = 6
            }).ToList();

            var outboundRanking = Enumerable.Range(1, 7).Select(i => new RankingItem
            {
                Rank = i,
                ProductName = $"货品{i}",
                Quantity = 45,
                Amount = 180.00m,
                Count = 5
            }).ToList();

            return new RankingData
            {
                InboundRanking = inboundRanking,
                OutboundRanking = outboundRanking
            };
        }

        public async Task<List<string>> GetSystemMessagesAsync(int count = 10)
        {
            _logger.LogInformation($"从WMS服务获取系统消息，数量：{count}");
            var messages = new List<string>
            {
                "您有新的销售订单需要审核 2025-01-22 16:18:34",
                "入库任务已完成 2025-01-22 15:30:22",
                "出库单据已审核通过 2025-01-22 14:45:10",
                "库存预警：商品A库存不足 2025-01-22 14:20:05",
                "调拨申请已提交 2025-01-22 13:55:30"
            };

            return messages.Take(count).ToList();
        }
    }
} 